package com.coco.area.controller;

import com.coco.area.pojo.TbArea;
import com.coco.area.service.AreaService;
import com.coco.common.annotation.UserLoginToken;
import com.coco.common.exception.CustomException;
import com.coco.common.exception.ErrorEnum;
import com.coco.common.result.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Package: com.coco.area.controller
 * @Author: 张dy
 * @Date: Created in 2021/7/19 下午 3:20
 */
@RestController
@CrossOrigin
@RequestMapping("/area")
public class AreaController {


    @Autowired
    private AreaService areaService;

    /**
     * 查找全部
     *
     * @Author: 张dy
     * @Date: 2021/7/19
     * @Param: []
     */
    @GetMapping("/findAll")
    public List<TbArea> findAll() {
        try {
            return areaService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(ErrorEnum.NOT_FOUND);
        }
    }

    /**
     * 通过id查找下级地域
     *
     * @Author: 张dy
     * @Date: 2021/7/19
     * @Param: [id]
     */
    @UserLoginToken
    @GetMapping("/selectByPid/{id}")
    public List<TbArea> selectByPid(@PathVariable("id") Integer id) {
        try {
            return areaService.selectByPid(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(ErrorEnum.NOT_FOUND);
        }
    }

    /**
     * 查找全部
     *
     * @Author: 张dy
     * @Date: 2021/7/19
     * @Param: []
     */
    @GetMapping("/findAllTree/{id}")
    public List<Map> findAllTree(@PathVariable("id") Integer id) {
        try {
            return areaService.findAllTree(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(ErrorEnum.NOT_FOUND);
        }
    }

}
